package dao;

import java.util.List;

import javax.persistence.CacheRetrieveMode;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.Hotel;
import model.Rorder;
import model.RorderPK;
import model.Route;
import model.Tourist;
import model.Hotel;
import model.Hroute;
import model.HroutePK;
import util.EntityManagerUtil;

public class HrouteDao {

	public static Hroute newHroute(String rname, String vname) throws Exception {
		Route route = RouteDao.getRouteNoCache(rname);
		Hotel hotel = HotelDao.getHotelNoCache(vname);
		Hroute hroute = new Hroute();
		HroutePK pk = new HroutePK();
		pk.setRName(route.getRname());
		pk.setHEmail(hotel.getEmail());
		hroute.setId(pk);
		return hroute;
	}

	public static boolean getHrouteNoCacheForBool(Hroute hroute) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Hroute c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Hroute HrouteFind;
		try {
			HrouteFind = (Hroute) query.setParameter("id", hroute.getId()).getSingleResult();
			em.close();
			return true;
		} catch (Exception e) {
			// e.printStackTrace();
			em.close();
			return false;
		}
	}

	// 查找信息无缓存
	public static Hroute getHrouteNoCache(Hroute hroute) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Hroute c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		try {
			Hroute hrouteFind = (Hroute) query.setParameter("id", hroute.getId()).getSingleResult();
			em.close();
			return hrouteFind;
		} catch (Exception e) {
			em.close();
			// TODO Auto-generated catch block
			System.out.println("查询Hroute失败");
			return null;
		}

	}

	public static boolean delHroute(Hroute hroute) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Hroute hrouteFind = getHrouteNoCache(hroute);
			if (hrouteFind != null) {
				Hroute delHroute = em.find(Hroute.class, hrouteFind.getId());
				em.getTransaction().begin();
				Route route = RouteDao.getRouteNoCache(hroute.getId().getRName());
				Hotel hotel = HotelDao.getHotelNoCache(hroute.getId().getHEmail());
				route.setMoney(route.getMoney() - hotel.getPrice());
				em.merge(route);
				em.remove(delHroute);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public static boolean addHroute(Hroute hroute) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			if (!getHrouteNoCacheForBool(hroute)) {
				em.getTransaction().begin();
				Route route = RouteDao.getRouteNoCache(hroute.getId().getRName());
				Hotel hotel = HotelDao.getHotelNoCache(hroute.getId().getHEmail());
				System.out.println("HrouteDao = "+hroute.getId().getRName() );
				System.out.println("HrouteDao = "+hroute.getId().getHEmail() );
				route.setMoney(route.getMoney() + hotel.getPrice());
				em.merge(route);
				em.merge(hroute);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
	
	public static boolean addHroute1(Hroute hroute) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			if (!getHrouteNoCacheForBool(hroute)) {
				em.getTransaction().begin();
				Route route = RouteDao.getRouteNoCache(hroute.getId().getRName());
				Hotel hotel = HotelDao.getHotelNoCache(hroute.getId().getHEmail());
				System.out.println("HrouteDao = "+hroute.getId().getRName() );
				System.out.println("HrouteDao = "+hroute.getId().getHEmail() );
				hroute.setHotel(hotel);
				hroute.setRoute(route);
				route.setMoney(route.getMoney() + hotel.getPrice());
				em.merge(route);
				em.merge(hroute);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
	public static void main(String args[]) throws Exception {
		Hroute hroute = newHroute("shililuxian2", "t1@qq.com");
		addHroute1(hroute);
	}
}
